package com.list.controller;

import com.alibaba.fastjson2.JSON;
import com.common.constant.RedisKey;
import com.common.domain.po.Video;
import com.common.result.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 路亚
 * @version 0.1
 * @className ListController
 * @description
 * @date 2025/1/2 16:48
 * @since jdk11
 */
@RestController
@RequestMapping("/list")
@Slf4j
@RequiredArgsConstructor
public class ListController {

    private final StringRedisTemplate stringRedisTemplate;

    @GetMapping("/soar")
    public Result soar() {
        String key = RedisKey.LIST_SOAR_RESULT_KEY;
        Set<String> jsonResult = stringRedisTemplate.opsForZSet().reverseRange(key, 0, 10);
        Map<Integer, Video> result = new HashMap<>();
        for (int i = 1; i <= jsonResult.size(); i++) {
            result.put(i, JSON.parseObject(jsonResult.toArray()[i-1].toString(), Video.class));
        }
        return Result.success(result);
    }

    @GetMapping("/search")
    public Result search() {
        String key = RedisKey.LIST_SEARCH_RESULT_KEY;
        Set<String> jsonResult = stringRedisTemplate.opsForZSet().reverseRange(key, 0, 10);
        Map<Integer, Video> result = new HashMap<>();
        for (int i = 1; i <= jsonResult.size(); i++) {
            result.put(i, JSON.parseObject(jsonResult.toArray()[i-1].toString(), Video.class));
        }
        return Result.success(result);
    }

    @GetMapping("/hot")
    public Result hot() {
        String key = RedisKey.LIST_HOT_RESULT_KEY;
        Set<String> jsonResult = stringRedisTemplate.opsForZSet().reverseRange(key, 0, 10);
        Map<Integer, Video> result = new HashMap<>();
        for (int i = 1; i <= jsonResult.size(); i++) {
            result.put(i, JSON.parseObject(jsonResult.toArray()[i-1].toString(), Video.class));
        }
        return Result.success(result);
    }
}
